<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use App\Model\CarBrand;
use Illuminate\Http\Request;

class BrandController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    //

    /**
     * 获取brands
     */
    public function brands( Request $request ){

        $get = $request->all();
//        $where = [];
//        if( !empty( $get['name'] ) ){
//            $where[] =[ 'name', 'like', '%' . $get['name'] .'%'];
//        }
//        if( !empty( $get['letter_pre'] ) ){
//            $where[] =[ 'letter_pre', 'like', '%' . $get['letter_pre'] .'%'];
//        }
        $brands = CarBrand::brands( $get );

        echo json_encode( $brands );
    }

    /**
     * 获取brand 根据brand_id
     */
    public function brand( $id ){

        $brand = CarBrand::brand($id);

        echo json_encode( $brand );
    }

    /**
     * 获取brand 根据brand_id
     */
    public function brand_edit( Request $request, $id ){

        $post = $request->all();
        $params = $post;
        if( !empty( $id ) ){
            $res = CarBrand::brand_edit($id, $params);
        }else{
            $car_brand = new CarBrand();
            $car_brand->name = $params['name'];
            $car_brand->letter_pre = $params['letter_pre'];
            $res = $car_brand->save();
        }

        echo json_encode( $res );
    }

}
